package cn.stylefeng.guns.modular.recy.controller;
/*
 */

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.core.base.controller.*;
import org.apache.commons.collections.ListUtils;
import org.springframework.stereotype.*;
import cn.stylefeng.guns.modular.recy.service.*;
import org.springframework.beans.factory.annotation.*;
import cn.stylefeng.roses.core.reqres.response.*;
import cn.stylefeng.guns.modular.recy.entity.*;
import java.io.*;
import cn.stylefeng.guns.core.common.page.*;
import com.baomidou.mybatisplus.core.metadata.*;
import com.baomidou.mybatisplus.core.conditions.*;
import cn.stylefeng.guns.modular.recy.vo.*;
import cn.stylefeng.guns.modular.recy.utils.*;
import cn.hutool.core.bean.*;
import java.util.stream.*;
import cn.stylefeng.guns.modular.recy.warpper.*;
import com.baomidou.mybatisplus.core.conditions.query.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.*;
import java.util.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.*;

@Controller
@RequestMapping({ "/region" })
public class RegionController extends BaseController
{
    private String PREFIX;
    @Autowired
    private RegionService regionService;
    
    public RegionController() {
        this.PREFIX = "/modular/recy/region/";
    }
    
    @RequestMapping({ "" })
    public String index() {
        return this.PREFIX + "region.html";
    }
    
    @RequestMapping({ "/add" })
    public String regionAdd() {
        return this.PREFIX + "region_add.html";
    }
    
    @RequestMapping({ "/edit" })
    public String regionUpdate(@RequestParam("id") final String id) {
        return this.PREFIX + "region_edit.html";
    }
    
    @RequestMapping({ "/detail/{id}" })
    @ResponseBody
    public ResponseData detail(@PathVariable final String id) {
        final Region region = (Region)this.regionService.getById((Serializable)id);
        return (ResponseData)ResponseData.success(region);
    }
    
    @RequestMapping({ "/list" })
    @ResponseBody
    public Object list(final Region regionQuery) {
        final QueryWrapper<Region> regionQueryWrapper = ControllerUtil.createQuery(regionQuery, this.getHttpServletRequest());
        final Page<Region> page = (Page<Region>)LayuiPageFactory.defaultPage();
        final IPage<Region> page2 = (IPage<Region>)this.regionService.page((IPage)page, (Wrapper)regionQueryWrapper);
        final List<RegionVO> regionVOList = EntityBind.bind(page2.getRecords(), RegionVO.class);
        final List<Map<String, Object>> list = regionVOList.stream().map(region -> BeanUtil.beanToMap(region)).collect(Collectors.toList());
        page2.setRecords((List)new RegionWrapper(list).wrap());
        return LayuiPageFactory.createPageInfo(page2);
    }
    
    @RequestMapping({ "/addItem" })
    @ResponseBody
    public Object add(final Region region) {
        this.regionService.save(region);
        return RegionController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/delete" })
    @ResponseBody
    public Object delete(@RequestParam final String id) {
        this.regionService.removeById((Serializable)id);
        return RegionController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/editItem" })
    @ResponseBody
    public Object update(final Region region) {
        this.regionService.updateById(region);
        return RegionController.SUCCESS_TIP;
    }
    
    @RequestMapping(value = { "/delItems" }, method = { RequestMethod.POST })
    @ResponseBody
    public Object delItem(@RequestBody final String[] ids) {
        this.regionService.removeByIds((Collection)Arrays.asList(ids));
        return RegionController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/show" })
    public Object show(final String id, final Model model) {
        final Region region = (Region)this.regionService.getById((Serializable)id);
        final Map map = BeanUtil.beanToMap(region);
        model.addAttribute("region", new RegionWrapper(map).wrap());
        return this.PREFIX + "region_show.html";
    }
    @RequestMapping("/search")
    @ResponseBody
    public Object search(String params){
        String[] regions = StrUtil.split(params,"/");
        if(ObjectUtil.isNotNull(regions) && regions.length>1) {
            QueryWrapper<Region> regionQueryWrapper = new QueryWrapper<>();
            regionQueryWrapper.lambda().eq(Region::getLevel,2).eq(Region::getName, regions[0]).or().eq(Region::getLevel,3).eq(Region::getName, regions[1]);
            if(regions.length>2) {
                regionQueryWrapper.lambda().or().eq(Region::getLevel,4).eq(Region::getName, regions[2]);
            }
            regionQueryWrapper.orderByAsc("level").orderByAsc("longcode");
            List<Region> list = this.regionService.list(regionQueryWrapper);
            if(list.size()>0) {
                Region province = list.get(0);
                list = list.stream().filter(region -> {
                    return StrUtil.startWith(region.getLongcode(), province.getLongcode());
                }).collect(Collectors.toList());
                Region lastRegion = CollectionUtil.getLast(list);
                if(lastRegion!=null) {
                    regionQueryWrapper = new QueryWrapper<>();
                    regionQueryWrapper.lambda().likeRight(Region::getLongcode,lastRegion.getLongcode()+"_").orderByAsc(Region::getLongcode);
                    List<Region> cunList = this.regionService.list(regionQueryWrapper);
                    if(cunList.size()>0) {
                        Region first = CollectionUtil.getFirst(cunList);
                        final Integer level = first.getLevel();
                        return cunList.stream().map(region -> {
                            return StrUtil.repeat("- - - -",region.getLevel()-level)+region.getName();
                        }).collect(Collectors.toList());
                    }else{
                        return new ArrayList<String>();
                    }
                }
            }
        }
        return new ArrayList<>();
    }

    @RequestMapping("/searchReturnCode")
    @ResponseBody
    public Object searchReturnCode(String params){
        String[] regions = StrUtil.split(params,"/");
        if(ObjectUtil.isNotNull(regions) && regions.length>1) {
            QueryWrapper<Region> regionQueryWrapper = new QueryWrapper<>();
            regionQueryWrapper.lambda().eq(Region::getLevel,2).eq(Region::getName, regions[0]).or().eq(Region::getLevel,3).eq(Region::getName, regions[1]);
            if(regions.length>2) {
                regionQueryWrapper.lambda().or().eq(Region::getLevel,4).eq(Region::getName, regions[2]);
            }
            regionQueryWrapper.orderByAsc("level").orderByAsc("longcode");
            List<Region> list = this.regionService.list(regionQueryWrapper);
            if(list.size()>0) {
                Region province = list.get(0);
                list = list.stream().filter(region -> {
                    return StrUtil.startWith(region.getLongcode(), province.getLongcode());
                }).collect(Collectors.toList());
                Region lastRegion = CollectionUtil.getLast(list);
                if(lastRegion!=null) {
                    regionQueryWrapper = new QueryWrapper<>();
                    regionQueryWrapper.lambda().likeRight(Region::getLongcode,lastRegion.getLongcode()+"_").orderByAsc(Region::getLongcode);
                    List<Region> cunList = this.regionService.list(regionQueryWrapper);
                    if(cunList.size()>0) {
                        Region first = CollectionUtil.getFirst(cunList);
                        final Integer level = first.getLevel();
                        return cunList.stream().collect(Collectors.toMap(Region::getLongcode,region -> {
                            return StrUtil.repeat("- - - -",region.getLevel()-level)+region.getName();
                        }));
                    }else{
                        return new HashMap<String,String>();
                    }
                }
            }
        }
        return new HashMap<String,String>();
    }
}
